package com.asm.DBloader;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ASM_FDayExpHedDS {
	
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;

	
	public ASM_FDayExpHedDS (Context context)
	{
		this.context = context;
		dbHelper = new ASM_DB(context);
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	
	public int Create(ASM_FDayExpHed det) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		
		 
	
		ContentValues values = new ContentValues();
		values.put(ASM_DB.asmDExpHed_RefNo, det.getAsmDExpHed_RefNo());
		values.put(ASM_DB.asmDExpHed_TxnDate, det.getAsmDExpHed_TxnDate());
		values.put(ASM_DB.asmDExpHed_RepName, det.getAsmDExpHed_RepName());
		values.put(ASM_DB.asmDExpHed_DealCode, det.getAsmDExpHed_DealCode());
		values.put(ASM_DB.asmDExpHed_CostCode, det.getAsmDExpHed_CostCode());
		values.put(ASM_DB.asmDExpHed_RegCode, det.getAsmDExpHed_RegCode());
		values.put(ASM_DB.asmDExpHed_AreaCode, det.getAsmDExpHed_AreaCode());
		values.put(ASM_DB.asmDExpHed_Remarks, det.getAsmDExpHed_Remarks());
		values.put(ASM_DB.asmDExpHed_AddUser, det.getAsmDExpHed_AddUser());
		values.put(ASM_DB.asmDExpHed_AddDate, det.getAsmDExpHed_AddDate());
		values.put(ASM_DB.asmDExpHed_AddMach, det.getAsmDExpHed_AddMach());
		values.put(ASM_DB.asmDExpHed_Longitide, det.getAsmDExpHed_Longitide());
		values.put(ASM_DB.asmDExpHed_Latitude, det.getAsmDExpHed_Latitude());
		values.put(ASM_DB.asmDExpHed_Issynced, det.getAsmDExpHed_Issynced());
		values.put(ASM_DB.asmDExpHed_RecordId, det.getAsmDExpHed_RecordId());
		values.put(ASM_DB.asmDExpHed_Address, det.getAsmDExpHed_Address());
		count = (int) dB.insert(ASM_DB.ASM_FDAYEXPHED, null, values);

		dB.close();
		return count;
		
	}
	
	public ArrayList<String> getExpHedAll() 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select refno,txndate from FDayExpHed";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Details.add(cursor.getString(0)+"-:-"+cursor.getString(1));
		//	Details.add(cursor.getString(1));
			
			
			
		}
		
		return Details;
	}
	
	
	public int Delete(String Refno)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_FDAYEXPHED, "RefNo = '"+Refno+"'", null);

		dB.close();
		return count;
		
	}
	
	public ArrayList<String> getfDayExpHed(String refNo) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select RefNo,txndate,Remarks from FDayExpHed where RefNo = '"+refNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			String RefNo = cursor.getString(1);
		
			
			
			Details.add(cursor.getString(0)+"~"+cursor.getString(1)+"~"+cursor.getString(2));
	
			
			
			
		}
		
		return Details;
	}
	
	
	public int Update(ASM_FDayExpHed det) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		
		 
	
		ContentValues values = new ContentValues();
		values.put(ASM_DB.asmDExpHed_RefNo, det.getAsmDExpHed_RefNo());
		values.put(ASM_DB.asmDExpHed_TxnDate, det.getAsmDExpHed_TxnDate());
		values.put(ASM_DB.asmDExpHed_RepName, det.getAsmDExpHed_RepName());
		values.put(ASM_DB.asmDExpHed_DealCode, det.getAsmDExpHed_DealCode());
		values.put(ASM_DB.asmDExpHed_CostCode, det.getAsmDExpHed_CostCode());
		values.put(ASM_DB.asmDExpHed_RegCode, det.getAsmDExpHed_RegCode());
		values.put(ASM_DB.asmDExpHed_AreaCode, det.getAsmDExpHed_AreaCode());
		values.put(ASM_DB.asmDExpHed_Remarks, det.getAsmDExpHed_Remarks());
		values.put(ASM_DB.asmDExpHed_AddUser, det.getAsmDExpHed_AddUser());
		values.put(ASM_DB.asmDExpHed_AddDate, det.getAsmDExpHed_AddDate());
		values.put(ASM_DB.asmDExpHed_AddMach, det.getAsmDExpHed_AddMach());
		values.put(ASM_DB.asmDExpHed_RecordId, det.getAsmDExpHed_RecordId());
		count = (int) dB.update(ASM_DB.ASM_FDAYEXPHED, values, "RefNo = '"+det.getAsmDExpHed_RefNo().toString()+"'", null);;

		dB.close();
		return count;
		
	}
	public ArrayList<String> getExpHedforLoad(String RefNo) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select txndate from FDayExpHed where RefNo = '"+RefNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Details.add(cursor.getString(0));
		//	Details.add(cursor.getString(1));
			
			
			
		}
		
		return Details;
	}
	
	public ArrayList<String> getExpHedforLoadRemarks(String RefNo) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select Remarks from FDayExpHed where RefNo = '"+RefNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Details.add(cursor.getString(0));
		//	Details.add(cursor.getString(1));
			
			
			
		}
		
		return Details;
	}
	
	

}
